package com.kaishengit.interceptor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class StatisticsTimerInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		long startime = System.currentTimeMillis();
		String action = invocation.getProxy().getActionName();
		String invo = invocation.invoke();
		
		long time = System.currentTimeMillis() - startime;
		Map<String, List<Long>> map = (Map<String, List<Long>>) invocation
				.getInvocationContext().getSession().get("StatisticsTimerList");
		if (map == null) {
			map = new HashMap<String, List<Long>>();
		}
		List<Long> list = map.get(action);
		
		if (list == null) {
			list = new ArrayList<Long>();
		}
		list.add(time);
		map.put(action, list);
		
		invocation.getInvocationContext().getSession()
				.put("StatisticsTimerList", map);
		return invo;
	}

}
